package com.horizon.service.endpoint.dto;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 消息类型.
 *
 * @author zhaoshb
 * @since 2022/12/25 15:15
 */
public enum EndpointMessageType {

    UNKNOWN((byte) 0), REQUEST((byte) 1), RESPONSE((byte) 2), INVALID((byte) -1);

    private static Map<Byte, EndpointMessageType> codeMap;

    static {
        codeMap = Arrays.stream(EndpointMessageType.values()).collect(Collectors.toMap(EndpointMessageType::getCode, Function.identity()));
    }

    EndpointMessageType(Byte code) {
        this.code = code;
    }

    byte code;

    public byte getCode() {
        return code;
    }

    public static EndpointMessageType getMessageType(Byte code) {
        return codeMap.getOrDefault(code, UNKNOWN);
    }

}
